package com.hua.store.web.servlet;

import com.hua.store.domin.Category;
import com.hua.store.service.CategoryService;
import com.hua.store.service.impl.CategoryServiceImpl;
import com.hua.store.utils.UUIDUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet(name = "AdminCategoryServlet", urlPatterns = "/AdminCategoryServlet")
public class AdminCategoryServlet extends BaseServlet {

    public String findAllCategory(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CategoryService service = new CategoryServiceImpl();
        List<Category> allCats = service.getAllCats();
        req.setAttribute("allCats", allCats);
        return "/admin/category/list.jsp";
    }

    //addCategoryUI
    public String addCategoryUI(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        return "/admin/category/add.jsp";
    }

    public String addCategory(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String cname = req.getParameter("cname");
        CategoryService service = new CategoryServiceImpl();
        Category category = new Category();
        category.setCname(cname);
        category.setCid(UUIDUtils.getId());
        service.addCategory(category);
        resp.sendRedirect("AdminCategoryServlet?method=findAllCategory");
        return null;
    }
}
